<!doctype html>
<html lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8">
    <meta name="baidu-site-verification" content="dIcXMeY8Ya">
    
    <title>使用模糊搜索神器 FZF 来提升办公效率 | OSKernelLAB</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
    <meta name="keywords" content="gatieme, 成坚, 内核, linux, kernel">
    <meta name="description" content="内核杂谈">

    
    <link rel="alternative" href="/atom.xml" title="OSKernelLAB" type="application/atom+xml">
    
    
    <link rel="icon" href="/favicon.ico">
    
    <link rel="stylesheet" href="/css/style.css?v=1615735433097">
    <!--[if lt IE 9]>
    <script src="/js/html5.js"></script>
    <![endif]-->
    
<script>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src="https://hm.baidu.com/hm.js?1a8f9a67050c5fdb4c73cbbc00124bd8"
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>


</head>
</html>
<body class="home">
    <!--[if lt IE 9]>
    <div class="browsehappy">
        当前网页 <strong>不支持</strong>
        你正在使用的浏览器. 为了正常的访问, 请 <a href="http://browsehappy.com/">升级你的浏览器</a>.
    </div>
    <![endif]-->

    <!-- 博客头部 -->
    <header class="header">
    <section class="container header-main">
        <div class="logo">
            <a href="/">
                <div class="cover">
                    <span class="name">OSKernelLAB</span>
                    <span class="description">OS内核实验室</span>
                </div>
            </a>
        </div>
        <div class="dropnav iconfont icon-nav" id="JELON__btnDropNav"></div>
        <ul class="menu hidden" id="JELON__menu">
            
            <li rel="/2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity/index.html" class="item ">
                <a href="/" title="首页" class="iconfont icon-home">&nbsp;首页</a>
            </li>
            
            <li rel="/2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity/index.html" class="item ">
                <a href="/archives/" title="归档" class="iconfont icon-archives">&nbsp;归档</a>
            </li>
            
            <li rel="/2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity/index.html" class="item ">
                <a href="/lab/" title="实验室" class="iconfont icon-archives">&nbsp;实验室</a>
            </li>
            
            <li rel="/2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity/index.html" class="item ">
                <a href="/navigation/" title="网址导航" class="iconfont icon-archives">&nbsp;网址导航</a>
            </li>
            
            <li rel="/2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity/index.html" class="item ">
                <a href="/about/" title="关于" class="iconfont icon-staff">&nbsp;关于</a>
            </li>
            
            <li rel="/2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity/index.html" class="item ">
                <a href="/comment/" title="留言" class="iconfont icon-comment">&nbsp;留言</a>
            </li>
            
        </ul>
        <div class="profile clearfix">
            <div class="feeds fl">
                
                
                <p class="links">
                    
                        <a href="https://github.com/gatieme" target="_blank">Github</a>
                        |
                    
                        <a href="https://github.com/gatieme/gatieme.github.io" target="_blank">Hosted by Github Pages</a>
                        
                    
                </p>
                <p class="sns">
                    
                        <a href="https://blog.csdn.net/gatieme" class="csdn" target="_blank"><b>■</b> CSDN 博客</a>
                    
                        <a href="https://www.facebook.com/gatieme" class="qqweibo" target="_blank"><b>■</b> Facebook</a>
                    
                    <a href="javascript: void(0);" class="wechat">
                        <b>■</b>
                        公众号
                        <span class="popover">
                            <img src="/img/wechat_mp.jpg" width="120" height="120" alt="我的微信订阅号">
                            <i class="arrow"></i>
                        </span>
                    </a>
                </p>
                
            </div>
            <div class="avatar fr">
                <img src="/img/gatieme.jpg" alt="avatar" title="Gatieme-(成坚)">
            </div>
        </div>
    </section>
</header>


    <!-- 博客正文 -->
    <div class="container body clearfix">
        <section class="content">
            <div class="content-main widget">
                <!-- 文章页 -->
<!-- 文章 -->
<article class="post article">
    <header class="text-center">
        <h3 class="post-title"><span>使用模糊搜索神器 FZF 来提升办公效率</span></h3>
    </header>
    <p class="post-meta text-center">
        gatieme 发表于
        <time datetime="2021-02-14T16:32:00.000Z">2021-02-15</time>
    </p>
    <div id="JELON__articlePostContent" class="post-content">
        <table>
<thead>
<tr>
<th>日期</th>
<th style="text-align:center">作者</th>
<th style="text-align:center">GitHub</th>
<th style="text-align:center">CSDN</th>
<th style="text-align:center">BLOG</th>
</tr>
</thead>
<tbody>
<tr>
<td>2021-02-15</td>
<td style="text-align:center"><a href="https://kernel.blog.csdn.net" target="_blank" rel="noopener">成坚-gatieme</a></td>
<td style="text-align:center"><a href="https://github.com/gatieme/AderXCoding/tree/master/system/tools/fzf" target="_blank" rel="noopener"><code>AderXCoding/system/tools/fzf</code></a></td>
<td style="text-align:center"><a href="https://blog.csdn.net/gatieme/article/details/113828826" target="_blank" rel="noopener">使用模糊搜索神器 FZF 来提升办公体验</a></td>
<td style="text-align:center"><a href="https://oskernellab.com/2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity">Using FZF to Improve Productivit</a></td>
</tr>
</tbody>
</table>
<p><br></p>
<h1 id="1-fzf-介绍"><a href="#1-fzf-介绍" class="headerlink" title="1 fzf 介绍"></a>1 fzf 介绍</h1><hr>
<p><a href="https://github.com/junegunn/fzf" target="_blank" rel="noopener">fzf</a> 是一款使用 GO 语言编写的交互式的命令行工具, 可以用来查找任何 列表内容、文件、历史命令、 本机绑定的host、 进程、 Git 分支、进程 等.</p>
<p>我们可以通过 FZF 结合 git 等工具来提升我们的办公体验.</p>
<p><a href="https://github.com/junegunn/fzf" target="_blank" rel="noopener">github 仓库地址</a></p>
<p><a href="https://github.com/junegunn/fzf" target="_blank" rel="noopener">https://github.com/junegunn/fzf</a></p>
<h1 id="2-fzf-安装"><a href="#2-fzf-安装" class="headerlink" title="2 fzf 安装"></a>2 fzf 安装</h1><hr>
<p>FZF 目前已经在各大发行版的源中集成了, 比如 ubuntu 19.10 之后, 可以直接使用 apt 进行安装</p>
<table>
<thead>
<tr>
<th>Package Manager</th>
<th>Linux Distribution</th>
<th>Command</th>
</tr>
</thead>
<tbody>
<tr>
<td>APK</td>
<td>Alpine Linux</td>
<td><code>sudo apk add fzf</code></td>
</tr>
<tr>
<td>APT</td>
<td>Debian 9+/Ubuntu 19.10+</td>
<td><code>sudo apt-get install fzf</code></td>
</tr>
<tr>
<td>Conda</td>
<td></td>
<td><code>conda install -c conda-forge fzf</code></td>
</tr>
<tr>
<td>DNF</td>
<td>Fedora</td>
<td><code>sudo dnf install fzf</code></td>
</tr>
<tr>
<td>Nix</td>
<td>NixOS, etc.</td>
<td><code>nix-env -iA nixpkgs.fzf</code></td>
</tr>
<tr>
<td>Pacman</td>
<td>Arch Linux</td>
<td><code>sudo pacman -S fzf</code></td>
</tr>
<tr>
<td>pkg</td>
<td>FreeBSD</td>
<td><code>pkg install fzf</code></td>
</tr>
<tr>
<td>pkg_add</td>
<td>OpenBSD</td>
<td><code>pkg_add fzf</code></td>
</tr>
<tr>
<td>XBPS</td>
<td>Void Linux</td>
<td><code>sudo xbps-install -S fzf</code></td>
</tr>
<tr>
<td>Zypper</td>
<td>openSUSE</td>
<td><code>sudo zypper install fzf</code></td>
</tr>
</tbody>
</table>
<p>当然也可以使用 git clone 的方式安装</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git clone --depth <span class="number">1</span> https:<span class="comment">//github.com/junegunn/fzf.git ~/.fzf</span></span><br><span class="line">~/.fzf/install</span><br></pre></td></tr></table></figure>
<p>关于更详细的安装方法可以参照 <a href="https://github.com/junegunn/fzf#installation" target="_blank" rel="noopener">Installation</a></p>
<h1 id="3-fzf-使用"><a href="#3-fzf-使用" class="headerlink" title="3 fzf 使用"></a>3 fzf 使用</h1><hr>
<h2 id="3-1-FZF-基本使用"><a href="#3-1-FZF-基本使用" class="headerlink" title="3.1 FZF 基本使用"></a>3.1 FZF 基本使用</h2><hr>
<p>fzf 默认会启用用户交互查找, 从标准输入流(STDIN)读取, 并将匹配内容输出到标准输出流(STDOUT)中:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">find * -type f | fzf</span><br></pre></td></tr></table></figure>
<p>fzf 如果没有接受标准输入流, 那么就会直接进行文件查找(不包含隐藏文件), 可以通过设置 <code>FZF_DEFAULT_COMMAND</code> 修改该默认动作: </p>
<h2 id="3-2-FZF-基本操作"><a href="#3-2-FZF-基本操作" class="headerlink" title="3.2 FZF 基本操作"></a>3.2 FZF 基本操作</h2><hr>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fzf # 直接输入 fzf, 打开文件搜索功能</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注: 在 fzf 的用户交互界面中, 用户的操作有如下动作可选: </p>
</blockquote>
<ul>
<li>使用CTRL-J/CTRL-K(或者CTRL-N/CTRL-P)进行上下选择</li>
<li>使用Enter选中条目, CTRL-C/CTRTRL-G/ESC进行退出操作</li>
<li>在多选择模式(-m), 使用TAB和Shift-TAB标记多个条目</li>
<li>Emacs 风格按键绑定</li>
<li>支持鼠标操作</li>
</ul>
<h2 id="3-3-搜索"><a href="#3-3-搜索" class="headerlink" title="3.3 搜索"></a>3.3 搜索</h2><hr>
<h3 id="3-3-1-搜索语法"><a href="#3-3-1-搜索语法" class="headerlink" title="3.3.1 搜索语法"></a>3.3.1 搜索语法</h3><hr>
<p>fzf 默认会以 “extened-search” 模式启动, 这种模式下不支持正则搜索, 但是你可以输入多个搜索关键词, 以空格分隔, fzf 会无序查找匹配所有字符串.</p>
<p>如 ^music .mp3$, sbtrkt !fire.</p>
<p>fzf 提供了一些增强功能的搜索语法, 如下表所示: </p>
<table>
<thead>
<tr>
<th>标记</th>
<th>匹配类型</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>sbtrkt</td>
<td>模糊匹配</td>
<td>内容匹配sbtrkt(字符匹配)</td>
</tr>
<tr>
<td>‘wild</td>
<td>精确匹配(单引号)</td>
<td>内容包含单词wild(单词匹配)</td>
</tr>
<tr>
<td>^music</td>
<td>前缀精确匹配</td>
<td>以music开头</td>
</tr>
<tr>
<td>.mp3$</td>
<td>后缀精确匹配</td>
<td>以.mp3结尾</td>
</tr>
<tr>
<td>!fire</td>
<td>反转匹配</td>
<td>内容不包含fire</td>
</tr>
<tr>
<td>!^music</td>
<td>前缀反转匹配</td>
<td>不以music开头</td>
</tr>
<tr>
<td>!.mp3$</td>
<td>后缀反转匹配</td>
<td>不以.mp3结尾</td>
</tr>
</tbody>
</table>
<blockquote>
<p>注: 如果不想使用模糊匹配或者不想”引用”每个文字, 可以使用 <code>-e/--exact</code> 选项. 注意如果使用 <code>-e/--exact</code>, 那么 <code>&#39;</code> 就变成了解引用, 即:’abc表示匹配a,b和c(a,b,c有序), 而不仅仅是匹配abc.</p>
</blockquote>
<h3 id="3-3-2-或操作"><a href="#3-3-2-或操作" class="headerlink" title="3.3.2 或操作"></a>3.3.2 或操作</h3><hr>
<p>fzf 以空格分隔, 默认使用的是 与 操作(无序), 如果想使用 或 操作, 那么可以使用 <code>|</code> : </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^core go$ | rb$ | py$ # 表示以`core`开头, 且以`go`或`rb`或`py`结尾</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注: | 前后必须带空格. </p>
</blockquote>
<h3 id="3-3-3-模糊补全"><a href="#3-3-3-模糊补全" class="headerlink" title="3.3.3 模糊补全"></a>3.3.3 模糊补全</h3><hr>
<p>在 bash 或 zsh 终端上, 可以通过输入 <code>**</code> 来触发 fzf 对文件/目录的模糊补全(查找), 如下例子所示:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># Files under current directory</span><br><span class="line"># - You can select multiple items with TAB key</span><br><span class="line">vim **&lt;TAB&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>进程 ID 模糊补全</li>
</ul>
<p>在使用kill命令时, fzf 会自动触发其自动补全功能: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># Can select multiple processes with &lt;TAB&gt; <span class="keyword">or</span> &lt;Shift-TAB&gt; keys</span><br><span class="line">kill <span class="number">-9</span> &lt;TAB&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>主机名补全</li>
</ul>
<p>如下例子所示: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ssh **&lt;TAB&gt;</span><br><span class="line">telnet **&lt;TAB&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>自定义模糊补全</li>
</ul>
<p>在 bash 上, fzf 的模糊补全功能只对一些预定义的命令集有效(具体命令集: <code>complete | grep _fzf</code>), 但是我们也可以为其他命令设置 fzf 模糊补全功能, 如下所示: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 为 rg 增加模糊补全, rg -F <span class="string">"def main("</span> **&lt;TAB&gt;</span><br><span class="line">complete -F _fzf_path_completion -o <span class="keyword">default</span> -o bashdefault rg</span><br><span class="line"></span><br><span class="line"># 为 tree 增加模糊补全, tree  **&lt;TAB&gt;</span><br><span class="line">complete -F _fzf_dir_completion -o <span class="keyword">default</span> -o bashdefault tree</span><br></pre></td></tr></table></figure>
<p>fzf 目前有提供相关 API 供我们自定义模糊补全功能, 具体步骤如下:<br>1) 首先自定义一个函数, 使用 fzf 提供的 API: <code>_fzf_complete</code> 提供补全功能: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># Custom fuzzy completion <span class="keyword">for</span> <span class="string">"doge"</span> command</span><br><span class="line"><span class="meta">#   e.g. doge **<span class="meta-string">&lt;TAB&gt;</span></span></span><br><span class="line">_fzf_complete_doge() &#123;</span><br><span class="line">  _fzf_complete <span class="string">"--multi --reverse"</span> <span class="string">"$@"</span> &lt; &lt;(</span><br><span class="line">    echo very</span><br><span class="line">    echo wow</span><br><span class="line">    echo such</span><br><span class="line">    echo doge</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>2) 在 bash 中, 使用complete指令链接我们的自定义函数: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[ -n <span class="string">"$BASH"</span> ] &amp;&amp; complete -F _fzf_complete_doge -o <span class="keyword">default</span> -o bashdefault doge</span><br></pre></td></tr></table></figure>
<p>3) 终端输入: <code>doge **&lt;TAB&gt;</code>, 即可验证结果</p>
<h2 id="3-4-按键绑定"><a href="#3-4-按键绑定" class="headerlink" title="3.4 按键绑定"></a>3.4 按键绑定</h2><hr>
<p>fzf 的安装脚本会为 bash, zsh 和 fish 终端设置以下按键绑定: </p>
<table>
<thead>
<tr>
<th>按键</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>CTRL-T</td>
<td>命令行打印选中内容</td>
</tr>
<tr>
<td>CTRL-R</td>
<td>命令行历史记录搜索, 并打印输出</td>
</tr>
<tr>
<td>ALT-C</td>
<td>模糊搜索目录, 并进入(cd)</td>
</tr>
</tbody>
</table>
<h2 id="3-5-环境变量"><a href="#3-5-环境变量" class="headerlink" title="3.5 环境变量"></a>3.5 环境变量</h2><hr>
<table>
<thead>
<tr>
<th>name</th>
<th>description example</th>
</tr>
</thead>
<tbody>
<tr>
<td>FZF_DEFAULT_COMMAND</td>
<td>输入为 tty 时的默认命令  export FZF_DEFAULT_COMMAND=’fd –type f’</td>
</tr>
<tr>
<td>FZF_DEFAULT_OPTS</td>
<td>设置默认选项  export FZF_DEFAULT_OPTS=”–layout=reverse –inline-info”</td>
</tr>
<tr>
<td>FZF_CTRL_T_COMMAND</td>
<td>按键映射<ctrl-t>行为设置</ctrl-t></td>
</tr>
<tr>
<td>FZF_CTRL_T_OPTS</td>
<td>按键映射<ctrl-t>选项设置</ctrl-t></td>
</tr>
<tr>
<td>FZF_CTRL_R_OPTS</td>
<td>按键映射<ctrl-r>选项设置</ctrl-r></td>
</tr>
<tr>
<td>FZF_ALT_C_COMMAND</td>
<td>按键映射<ctrl-c>行为设置</ctrl-c></td>
</tr>
<tr>
<td>FZF_ALT_C_OPTS</td>
<td>按键映射<ctrl-c>选项设置</ctrl-c></td>
</tr>
</tbody>
</table>
<p>fzf 默认会以全屏方式显示交互界面, 可以使用–height选项设置交互界面高度: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim $(fzf --height <span class="number">40</span>%)</span><br></pre></td></tr></table></figure>
<p>可以通过设置$FZF_DEFAULT_OPTS变量更改 fzf 默认行为: </p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 设置 fzf 默认交互界面大小</span><br><span class="line">export FZF_DEFAULT_OPTS='--height 40%'</span><br></pre></td></tr></table></figure>
<h2 id="3-6-预览窗口"><a href="#3-6-预览窗口" class="headerlink" title="3.6 预览窗口"></a>3.6 预览窗口</h2><hr>
<p>可以通过提供 <code>--preview</code> 选项打开预览窗口, 并设置响应命令输出到预览窗口上.</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># &#123;&#125; is replaced to the single-quoted <span class="built_in">string</span> of the focused line</span><br><span class="line">fzf --preview 'cat &#123;&#125;' # 预览文件内容</span><br><span class="line">fzf --preview 'rg -F "def main(" -C 3 &#123;&#125;' # 预览 Python 文件 main 函数前后3行代码</span><br></pre></td></tr></table></figure>
<h2 id="3-7-高级配置"><a href="#3-7-高级配置" class="headerlink" title="3.7 高级配置"></a>3.7 高级配置</h2><hr>
<h3 id="3-7-1-更改查找引擎"><a href="#3-7-1-更改查找引擎" class="headerlink" title="3.7.1 更改查找引擎"></a>3.7.1 更改查找引擎</h3><hr>
<p>默认情况下, fzf 使用的查找引擎是系统自带的 <code>find</code> 命令, 这里我们可以对其进行更改, 换成更高效的查找引擎. </p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 使用 rg 进行搜索</span><br><span class="line">export FZF_DEFAULT_COMMAND='rg --files --hidden'</span><br></pre></td></tr></table></figure>
<h3 id="3-7-2-自定义命令"><a href="#3-7-2-自定义命令" class="headerlink" title="3.7.2 自定义命令"></a>3.7.2 自定义命令</h3><hr>
<p>我们可以通过设置按键映射在 fzf 交互界面直接开启外部进程 (<code>execute, execute-silent</code>) 运行我们选中的文件</p>
<ul>
<li>通过快捷键执行外部程序</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 在交互界面选中文件后, 按下 F1, 直接使用 vim 打开</span><br><span class="line">fzf --bind 'f1:execute(vim &#123;&#125;)'</span><br></pre></td></tr></table></figure>
<ul>
<li>简化命令</li>
</ul>
<p>可以通过定义 shell 脚本简化 fzf 命令执行. 比如, 下面示例定义了一个函数, 结合ag实现传参进行模糊搜索, 并用 vim 打开:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># fuzzy grep open via ag</span></span><br><span class="line">vg() &#123;</span><br><span class="line">  local file</span><br><span class="line"></span><br><span class="line">  file=<span class="string">"$(ag --nobreak --noheading $@ | fzf -0 -1 | awk -F: '&#123;print $1&#125;')"</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> [[ -n $file ]]</span><br><span class="line">  then</span><br><span class="line">     vim $file</span><br><span class="line">  fi</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>自定义全局快捷键触发</li>
</ul>
<p>像 fzf 其实已经有为我们提供了一些按键映射, 比如<ctrl-t>, 可以打印出选中文件. 现在我们也仿照写一个该功能脚本, 全局快捷键设为<ctrl-g><ctrl-o>:</ctrl-o></ctrl-g></ctrl-t></p>
<p>1) 首先先写目录搜索(fzf)并打印输出功能脚本: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># .bashrc</span><br><span class="line">outputDir() &#123;</span><br><span class="line">    local dir</span><br><span class="line">    dir=$(find $&#123;<span class="number">1</span>:-.&#125; -type d <span class="number">2</span>&gt; /dev/null | fzf +m)</span><br><span class="line">    echo $dir</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>2)然后进行全局按键映射: </p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># .bashrc</span><br><span class="line">bind '"\er": redraw-current-line'</span><br><span class="line">bind '"\C-g\C-o": "$(outputDir)\e\C-e\er"'</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注:<br>1) \e\C-e: shell-expand-line默认按键绑定, 这是最容易的方式进行按键绑定, 缺点就是它对于别名(alias)也会同样进行展开.<br>2) redraw-current-line: 在非 tmux 终端上, 该选项必须存在, 否则无法清除提示.<br>3) 先source .bashrc, 然后按快捷键: <ctrl-g><ctrl-o>, 运行结果如下: </ctrl-o></ctrl-g></p>
</blockquote>
<h3 id="3-7-3-为预览窗口增加语法高亮"><a href="#3-7-3-为预览窗口增加语法高亮" class="headerlink" title="3.7.3 为预览窗口增加语法高亮:"></a>3.7.3 为预览窗口增加语法高亮:</h3><hr>
<p>预览窗口支持 ANSI 颜色, 因此我们可以为文件内容增加语法高亮. 我们借助 bat 这个库来为我们的文本显示语法高亮功能: </p>
<blockquote>
<p>建议安装bat， 是一个Rust编写的独立程序<br>也可以安装rougify(先安装ruby, 然后gem intall rouge)</p>
</blockquote>
<p>1) 首先, 安装 bat 库. 具体步骤请查看文档.<br>2) 终端输入以下内容: </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">fzf --preview '[[ $(file --mime &#123;&#125;) =~ binary ]] &amp;&amp;</span><br><span class="line">                 echo &#123;&#125; is a binary file ||</span><br><span class="line">                 (bat --style=numbers --color=always &#123;&#125; ||</span><br><span class="line">                  highlight -O ansi -l &#123;&#125; ||</span><br><span class="line">                  coderay &#123;&#125; ||</span><br><span class="line">                  rougify &#123;&#125; ||</span><br><span class="line">                  cat &#123;&#125;) <span class="number">2</span>&gt; /dev/null | head <span class="number">-500'</span></span><br></pre></td></tr></table></figure>
<p>结果如下: </p>
<h1 id="4-用-FZF-提升其他工具的效率"><a href="#4-用-FZF-提升其他工具的效率" class="headerlink" title="4 用 FZF 提升其他工具的效率"></a>4 用 FZF 提升其他工具的效率</h1><hr>
<h2 id="4-1-Git-的好帮手"><a href="#4-1-Git-的好帮手" class="headerlink" title="4.1 Git 的好帮手"></a>4.1 Git 的好帮手</h2><hr>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">#---------------------</span><br><span class="line"><span class="meta"># modify by gatieme at 2021-02-01 20:47 for FZF</span></span><br><span class="line">#---------------------</span><br><span class="line"><span class="meta">#export FZF_CTRL_T_OPTS=<span class="meta-string">"--layout=reserve --preview '(highlight -O ansi -l &#123;&#125; 2&gt; /dev/null || cat &#123;&#125; || tree -C &#123;&#125;) 2&gt; /dev/null | head -200'"</span></span></span><br><span class="line"><span class="meta">#export FZF_DEFAULT_OPTS=<span class="meta-string">"--height 99% -e --layout=reverse --preview '(highlight -O ansi -l &#123;&#125;' --color 'fg:#bbccdd,fg+:#ddeeff,bg:#334455,preview-bg:#223344,border:#778899'"</span></span></span><br><span class="line"><span class="keyword">export</span> FZF_DEFAULT_OPTS=<span class="string">"--height 99% -e --layout=reverse --preview '(bat --style=numbers --color=always &#123;&#125; ||  highlight -O ansi -l &#123;&#125; || coderay &#123;&#125; || rougify &#123;&#125; || cat &#123;&#125;) 2&gt; /dev/null '  --color 'fg:#bbccdd,fg+:#ddeeff,bg:#334455,preview-bg:#223344,border:#778899'"</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">function fview()</span><br><span class="line">&#123;</span><br><span class="line">  fzf -e --layout=reserve --preview=<span class="string">"~/.file_view.sh &#123;$2&#125; $1"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function lsf()</span><br><span class="line">&#123;</span><br><span class="line">  ls $<span class="number">1</span> | fview $<span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function cdf()</span><br><span class="line">&#123;</span><br><span class="line">  cd $(fdfind --type directory | fzf)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function gitlog()</span><br><span class="line">&#123;</span><br><span class="line">  git <span class="built_in">log</span> --oneline $* | fzf -e --multi --preview=<span class="string">"git show &#123;+1&#125;"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function gitblame()</span><br><span class="line">&#123;</span><br><span class="line">  git blame $<span class="number">1</span> | fzf -e --layout=reverse --preview=<span class="string">"git show &#123;1&#125;"</span> $<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function gitcheckout()</span><br><span class="line">&#123;</span><br><span class="line">  git checkout $(git branch -a) | fzf --preview=<span class="string">"git log --oneline &#123;1&#125;"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当然大家可以使用别的大神已经集成好的配置 <a href="https://github.com/wfxr/forgit" target="_blank" rel="noopener">wfxr/forgit</a></p>
<h2 id="4-2-VIM-大杀器"><a href="#4-2-VIM-大杀器" class="headerlink" title="4.2 VIM 大杀器"></a>4.2 VIM 大杀器</h2><hr>
<p>fzf 本身并不是一个vim 插件, 本来作者只提供了基本的wrapper函数(比如fzf#run). 但后来作者发现很多人并不熟悉 VIMScript, 所以就创建一个默认的 <a href="https://github.com/junegunn/fzf.vim" target="_blank" rel="noopener">vim plugin</a>.</p>
<p>如果对FZF和vim和结合感兴趣可以看： <a href="https://segmentfault.com/a/1190000016186540" target="_blank" rel="noopener">VIM与模糊搜索神器FZF的集成用法 - 从简单到高级</a></p>
<h1 id="5-参考资料"><a href="#5-参考资料" class="headerlink" title="5 参考资料"></a>5 参考资料</h1><hr>
<h2 id="5-1-中文资料"><a href="#5-1-中文资料" class="headerlink" title="5.1 中文资料"></a>5.1 中文资料</h2><hr>
<p>由于开始对于 FZF 不甚了解, 因此本博文较多参考了 <a href="https://www.jianshu.com/p/b48131e4ad06" target="_blank" rel="noopener">fzf - 命令行模糊搜索神器</a> 和 <a href="https://segmentfault.com/a/1190000011328080" target="_blank" rel="noopener">模糊搜索神器fzf</a>. 在这里对这两位作者的成果表示尊重和感谢. 鄙人同一时间正在写其他技术博文, 为了保证其他博文的细致, 因此这篇略显粗糙, 敬请谅解.</p>
<p><a href="https://github.com/junegunn/fzf/wiki" target="_blank" rel="noopener">FZF wiki</a></p>
<p><a href="https://segmentfault.com/a/1190000016186043" target="_blank" rel="noopener">模糊搜索神器FZF番外篇</a></p>
<h2 id="5-2-外文资料"><a href="#5-2-外文资料" class="headerlink" title="5.2 外文资料"></a>5.2 外文资料</h2><hr>
<p><a href="https://pragmaticpineapple.com/improving-vim-workflow-with-fzf/" target="_blank" rel="noopener">Improving Vim Workflow With fzf</a></p>
<p><a href="https://medium.com/akatsuki-taiwan-technology/using-fzf-to-improve-productivity-84da1df28163" target="_blank" rel="noopener">Using FZF to Improve Productivity</a></p>
<p><a href="https://medium.com/better-programming/boost-your-command-line-productivity-with-fuzzy-finder-985aa162ba5d" target="_blank" rel="noopener">Boost Your Command-Line Productivity With Fuzzy Finder</a></p>
<p><a href="https://www.freecodecamp.org/news/fzf-a-command-line-fuzzy-finder-missing-demo-a7de312403ff" target="_blank" rel="noopener">Why you should be using fzf, the command line fuzzy finder</a></p>
<p><a href="https://revelry.co/resources/development/terminal-workflow-fzf" target="_blank" rel="noopener">Improve Your Workflow in the Terminal with These fzf Tips</a></p>
<p>本作品采用<a rel="noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>进行许可, 转载请注明出处, 谢谢合作</p>
<p><a rel="noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png"></a></p>
<p>因本人技术水平和知识面有限, 内容如有纰漏或者需要修正的地方, 欢迎大家指正, 鄙人在此谢谢啦</p>
<p><strong>转载请务必注明出处, 谢谢, 不胜感激</strong></p>
<p><br></p>
<p><br></p>
<ul>
<li><p>本作品/博文 ( <a href="http://blog.csdn.net/gatieme" target="_blank" rel="noopener">AderStep-紫夜阑珊-青伶巷草 Copyright ©2013-2017</a> ), 由 <a href="http://blog.csdn.net/gatieme" target="_blank" rel="noopener">成坚(gatieme)</a> 创作.</p>
</li>
<li><p>采用<a rel="noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png"></a><a rel="noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>进行许可. 欢迎转载、使用、重新发布, 但务必保留文章署名<a href="http://blog.csdn.net/gatieme" target="_blank" rel="noopener">成坚gatieme</a> ( 包含链接: <a href="http://blog.csdn.net/gatieme" target="_blank" rel="noopener">http://blog.csdn.net/gatieme</a> ), 不得用于商业目的.</p>
</li>
<li><p>基于本文修改后的作品务必以相同的许可发布. 如有任何疑问, 请与我联系.</p>
</li>
<li><p><strong>转载请务必注明出处, 谢谢, 不胜感激</strong><br><br></p>
</li>
</ul>

    </div>
    <p class="eof">-- EOF --</p>
    <p class="post-meta">
        <span class="post-cat">分类：
            <a class="cat-link" href="/categories/技术积累/">技术积累</a>
        </span>
        <span class="post-tags">
            标签：
            
    
        <a href="/tags/linux/" title="linux">linux</a> / 
    
        <a href="/tags/tools/" title="tools">tools</a>
    

        </span>
    </p>
</article>
<!-- 分享按钮 -->

  <div class="article-share clearfix text-center">
    <div class="share-area">
      <span class="share-txt">分享到：</span>
      <a href="javascript: window.open('http://service.weibo.com/share/share.php?url=' + encodeURIComponent(location.href) + '&title=' + document.title + '&language=zh_cn');" class="share-icon weibo"></a>
      <a href="javascript: alert('请复制链接到微信并发送');" class="share-icon wechat"></a>
      <a href="javascript: window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + encodeURIComponent(location.href) + '&title=' + document.title);" class="share-icon qqzone"></a>
      <a href="javascript: window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + encodeURIComponent(location.href) + '&desc=Gatieme个人博客&title=' + document.title + '&callback=' + encodeURIComponent(location.href));" class="share-icon qq"></a>
      <a href="javascript: window.open('http://shuo.douban.com/!service/share?href=' + encodeURIComponent(location.href) + '&name=' + document.title + '&text=' + document.title);" class="share-icon douban"></a>
    </div>
  </div>



<!-- 上一篇/下一篇 -->

<div class="article-nav clearfix">
    
    <span class="prev fl">
        上一篇<br>
        <a href="javascript: void(0);">没有上一篇了</a>
    </span>
    

    
    <span class="next fr">
        下一篇<br>
        <a href="/2021/01/24/2021/0127-0001-Topdown_analysis_as_performed_on_Intel_CPU_using_pmu-tools/">
            
                Intel CPU 上使用 pmu-tools 进行 TopDown 分析
            
        </a>
    </span>
    
</div>

<!-- 文章评论 -->

  <script src="/js/comment.js?v=1615735432562"></script>
  <div id="comments" class="comment">
    <!--
    <div class="sign-bar">
      GitHub 已登录!
      <span class="sign-link">登出</span>
    </div>
    <section class="box">
      <div class="com-avatar"><img src="/img/gatieme.jpg" alt="avatar"></div>
      <div class="com-text">
        <div class="main">
          <textarea class="text-area-edited show" placeholder="欢迎评论！"></textarea>
          <div class="text-area-preview"></div>
        </div>
        <div class="switch">
          <div class="switch-item on">编辑</div>
          <div class="switch-item">预览</div>
        </div>
        <div class="button">提交</div>
      </div>
    </section>
    <section class="tips">注：评论支持 markdown 语法！</section>
    <section class="list-wrap">
      <ul class="list">
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/gatieme.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">张德龙</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like liked">已赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">333333</div>
          </div>
        </li>
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/gatieme.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">刘德华</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like">点赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">vvvvv</div>
          </div>
        </li>
      </ul>
      <div class="page-nav">
        <a href="javascript: void(0);" class="item">1</a>
        <a href="javascript: void(0);" class="item">2</a>
        <a href="javascript: void(0);" class="item current">3</a>
      </div>
    </section>
    -->
  </div>
  <script>
  JELON.Comment({
    container: 'comments',
    label: '2021/0215-0001-Using_FZF_to_Improve_Productivity' || '2021/02/15/2021/0215-0001-Using_FZF_to_Improve_Productivity/',
    owner: 'gatieme',
    repo: 'blog_comments',
    clientId: 'cf796c25fcf4369b9295',
    clientSecret: 'c2ecfa50b9914a8fc9655ede7c9e6cf2ff84a70c'
  });
  </script>




            </div>
        </section>
        <!-- 侧栏部分 -->
<aside class="sidebar">
    
    <section class="widget">
        <h3 class="widget-hd"><strong>文章搜索</strong></h3>
        <div class="search-form">
  <form id="searchForm" method="GET" action="https://www.google.com/search" ectype="application/x-www-form-urlencoded" target="_blank" autocomplete="false" onsubmit="javascript: return false;">
    <input id="searchKeyword" type="text" class="form-control" placeholder="输入关键字搜索" autocomplete="false">
    <input id="searchKeywordHidden" type="hidden" name="q">
    <input id="searchButton" class="btn" type="submit" value="搜索">
  </form>
</div>

    </section>
    

    <section class="widget">
        <h3 class="widget-hd"><strong>文章分类</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
    <li>
        <a href="/categories/linux/">linux</a>
        <span class="badge">(0)</span>
    </li>
    
    <li>
        <a href="/categories/hexo/">hexo</a>
        <span class="badge">(0)</span>
    </li>
    
    <li>
        <a href="/categories/qemu/">qemu</a>
        <span class="badge">(0)</span>
    </li>
    
    <li>
        <a href="/categories/debug/">debug</a>
        <span class="badge">(0)</span>
    </li>
    
    <li>
        <a href="/categories/编程珠玑/">编程珠玑</a>
        <span class="badge">(7)</span>
    </li>
    
    <li>
        <a href="/categories/技术积累/">技术积累</a>
        <span class="badge">(3)</span>
    </li>
    
    <li>
        <a href="/categories/内核探秘/">内核探秘</a>
        <span class="badge">(3)</span>
    </li>
    
</ul>
    </section>

    
    <section class="widget">
        <h3 class="widget-hd"><strong>热门标签</strong></h3>
        <!-- 文章标签 -->
<div class="widget-bd tag-wrap">
  
    <a class="tag-item" href="/tags/linux/" title="linux">linux (10)</a>
  
    <a class="tag-item" href="/tags/debug/" title="debug">debug (6)</a>
  
    <a class="tag-item" href="/tags/tools/" title="tools">tools (4)</a>
  
    <a class="tag-item" href="/tags/scheduler/" title="scheduler">scheduler (3)</a>
  
    <a class="tag-item" href="/tags/hexo/" title="hexo">hexo (2)</a>
  
    <a class="tag-item" href="/tags/blog/" title="blog">blog (2)</a>
  
    <a class="tag-item" href="/tags/qemu/" title="qemu">qemu (1)</a>
  
    <a class="tag-item" href="/tags/topdown/" title="topdown">topdown (1)</a>
  
    <a class="tag-item" href="/tags/kernel/" title="kernel">kernel (1)</a>
  
</div>
    </section>
    

    

    
    <!-- 友情链接 -->
    <section class="widget">
        <h3 class="widget-hd"><strong>友情链接</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
        <li>
            <a href="https://blog.csdn.net/gatieme" target="_blank" title="我的 CSDN 博客">CSDN--紫夜阑珊-青伶巷草</a>
        </li>
    
        <li>
            <a href="https://xydh.fun/gatieme" target="_blank" title="我的炫猿导航主页">炫猿导航</a>
        </li>
    
        <li>
            <a href="https://jelon.info" target="_blank" title="Jelon 的博客">Jelon 的博客</a>
        </li>
    
</ul>
    </section>
    
</aside>
<!-- / 侧栏部分 -->
    </div>

    <!-- 博客底部 -->
    <footer class="footer">
    &copy;
    
        2014-2021
    

    <a href="/">紫夜阑珊-青伶巷草</a>
</footer>
<div class="back-to-top" id="JELON__backToTop" title="返回顶部">返回顶部</div>

    <!--博客js脚本 -->
    <!-- 这里放网站js脚本 -->
<script src="/js/main.js?v=1615735433106"></script>

</body>
</html>
